/**
 * Created by bell on 2017/7/31.
 */

import AView from '../Views/AView'

export default  class AViewController {
    constructor() {

    }

    get view() : AView {
        if (!this._view) {
            this.loadView()
            this.viewDidLoad()
        }
        return this._view
    }

    set view(view : AView) {
        this._view = view;
    }

    loadView() {
        this.view = new AView()
    }

    viewDidLoad() {

    }
}


//private
AViewController.prototype.update = function() {

    let time =  new Date().getTime()

    if(!this._canvas) {
        let canvas = document.createElement('canvas')
        let view = this.view
        canvas.width  = view.frame.w
        canvas.height = view.frame.h
        canvas.style.position = 'absolute'

        let body = document.getElementsByTagName('body')[0];
        body.appendChild(canvas)

        this._canvas = canvas
    }
    let time1 =  new Date().getTime()
    console.log("time 1 "+ (time1 - time)/1000.0)

    if(this._canvas.width !== this.view.frame.w
        || this._canvas.height !== this.view.frame.h) {
        this._canvas.width = this.view.frame.w
        this._canvas.height = this.view.frame.h
    }

    let time2 =  new Date().getTime()
    console.log("time 2 "+ (time2 - time1)/1000.0)

    var context = this._canvas.getContext('2d');
    this.view.redraw = (view) => {
        view.draw(context, view.frame)
    }
    let time3 =  new Date().getTime()
    console.log("time 3 "+ (time3 - time2)/1000.0)
    this.view.redraw(this.view)
    let time4 =  new Date().getTime()
    console.log("time 4 "+ (time4 - time3)/1000.0)
}
